<template>
<div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="景区名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入景区名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="所在地" prop="addrProvince">
            <el-cascader size="large" :options="areaoptions" v-model="selectedOptions" @change="handleChange">
            </el-cascader>
        </el-form-item>
        <!-- <el-form-item label="是否开通" prop="ifAvailable">
        <el-select v-model="queryParams.ifAvailable" placeholder="请选择是否开通" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否付费" prop="ifPaid">
        <el-select v-model="queryParams.ifPaid" placeholder="请选择是否付费" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
        <el-form-item label="景区类型" prop="sortId">
            <el-select v-model="queryParams.sortId" filterable placeholder="请选择景区类型" clearable>
                <el-option v-for="dict in dict.type.cxb_scene_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['scenebase:scenebase:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['scenebase:scenebase:edit']">修改</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scenebase:scenebase:remove']"
        >删除</el-button>
      </el-col> -->
        <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['scenebase:scenebase:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scenebaseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="景区名称" align="center" prop="name" />
        <!-- <el-table-column label="景区描述" align="center" prop="description" /> -->
        <el-table-column label="景区电话" align="center" prop="telephone" />
        <el-table-column label="所在地-省级" align="center" prop="addrProvince" />
        <el-table-column label="所在地-市" align="center" prop="addrCity" />
        <el-table-column label="是否显示电话" align="center" prop="ifShowTel">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ifShowTel" />
            </template>
        </el-table-column>
        <!--<el-table-column label="首页显示标志 " align="center" prop="ifShowInMainpage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ifShowInMainpage"/>
        </template>
      </el-table-column>
       <el-table-column label="是否开通" align="center" prop="ifAvailable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ifAvailable"/>
        </template>
      </el-table-column>
      <el-table-column label="是否付费" align="center" prop="ifPaid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ifPaid"/>
        </template>
      </el-table-column>  -->
        <el-table-column label="景区类型" align="center" prop="sortId">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.cxb_scene_type" :value="scope.row.sortId" />
            </template>
        </el-table-column>
        <el-table-column label="景区标签" align="center" prop="tagId">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.cxb_scene_tag" :value="scope.row.tagId ? scope.row.tagId.split(',') : []" />
            </template>
        </el-table-column>
        <el-table-column label="景区图片" align="center" prop="picUrl">
            <template slot-scope="scope">
                <el-popover placement="top-start" trigger="click">
                    <a :href="scope.row.picUrl" target="_blank" title="查看">
                        <img :src="scope.row.picUrl" style="width: 500px;height: 400px">
                    </a>
                    <img slot="reference" :src="scope.row.picUrl" style="width: 50px;height: 50px; cursor:pointer">
                </el-popover>
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['scenebase:scenebase:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['scenebase:scenebase:remove']">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改景区基础数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="景区名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入景区名称" />
            </el-form-item>
            <el-form-item label="景区描述" prop="description">
                <!-- <el-input v-model="form.description" placeholder="请输入景区描述" /> -->
                <editor v-model="form.description" :min-height="192" />
            </el-form-item>
            <el-form-item label="景区电话" prop="telephone">
                <el-input v-model="form.telephone" placeholder="请输入景区电话" />
            </el-form-item>
            <el-form-item label="景区别名" prop="name2">
                <el-input v-model="form.name2" placeholder="请输入景区别名" />
            </el-form-item>
            <!-- <el-form-item label="所在地-省级" prop="addrProvince">
          <el-input v-model="form.addrProvince" placeholder="请输入所在地-省级" />
        </el-form-item>
        <el-form-item label="所在地-市" prop="addrCity">
          <el-input v-model="form.addrCity" placeholder="请输入所在地-市" />
        </el-form-item> -->
            <el-form-item label="所在地" prop="selectedAreaOptions">
                <el-cascader size="large" :options="areaoptions" v-model="form.selectedAreaOptions" @change="handleFormAreaChange">
                </el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="addrAddr">
                <el-input v-model="form.addrAddr" placeholder="请输入详细地址" />
            </el-form-item>
            <el-form-item label="是否显示电话" prop="ifShowTel">
                <el-select v-model="form.ifShowTel" placeholder="请选择是否显示电话">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="纬度" prop="latitude">
                <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
            <el-form-item label="经度" prop="longtitude">
                <el-input v-model="form.longtitude" placeholder="请输入经度" />
            </el-form-item>
            <el-form-item label="首页显示标志 " prop="ifShowInMainpage">
                <el-select v-model="form.ifShowInMainpage" placeholder="请选择首页显示标志 ">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否开通" prop="ifAvailable">
                <el-select v-model="form.ifAvailable" placeholder="请选择是否开通">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否付费" prop="ifPaid">
                <el-select v-model="form.ifPaid" placeholder="请选择是否付费">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="景区类型" prop="sortId">
                <el-select v-model="form.sortId" placeholder="请选择景区类型">
                    <el-option v-for="dict in dict.type.cxb_scene_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="景区标签">
                <el-checkbox-group v-model="form.tagId">
                    <el-checkbox v-for="dict in dict.type.cxb_scene_tag" :key="dict.value" :label="dict.value">
                        {{dict.label}}
                    </el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="链接appid" prop="linkAppId">
                <el-input v-model="form.linkAppId" placeholder="请输入链接appid" />
            </el-form-item>
            <el-form-item label="链接apppath" prop="linkAppPath">
                <el-input v-model="form.linkAppPath" placeholder="请输入链接apppath" />
            </el-form-item>
            <el-form-item label="景区图片" prop="picUrl">
                <!-- <el-input v-model="form.picUrl" type="textarea" placeholder="请输入内容" /> -->
                <el-upload class="upload-demo" :limit="1" :action="uploadFileUrl" :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" :on-success="handleUploadSuccess" :data="updata" :headers="headers" :file-list="fileList">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">不超过1Mb</div>
                </el-upload>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import {
    listScenebase,
    getScenebase,
    delScenebase,
    addScenebase,
    updateScenebase
} from "@/api/scenebase/scenebase";
import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode
} from 'element-china-area-data';
import {
    getToken
} from "@/utils/auth";
export default {
    name: "Scenebase",
    dicts: ['sys_vip_type', 'sys_yes_no', 'cxb_scene_tag', 'cxb_scene_type'],
    data() {
        return {
            fileList: [],
            baseUrl: process.env.VUE_APP_BASE_API,
            uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/uploadFile", // 上传的图片服务器地址
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            uploadObjectUrl: "", // 纯前端预览地址-upload
            uploadOnlineUrl: "", // 后端返回的上传地址-upload
            updata: {
                "strPath": "pics"
            },
            // 省市
            areaoptions: provinceAndCityData,
            selectedOptions: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            picUrl: null,
            // 景区基础数据表格数据
            scenebaseList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                addrProvince: null,
                addrCity: null,
                ifAvailable: null,
                ifPaid: null,
                sortId: null,
            },
            // 表单参数
            form: {},
            // 表单校验

            rules: {
                name: [{
                    required: true,
                    message: "景区名称不能为空",
                    trigger: "blur"
                }],
                selectedAreaOptions: [{
                    type: 'array',
                    required: true,
                    message: '请选择所在地',
                    trigger: 'change'
                }]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        },
        handleUploadSuccess(res, file) {
            this.form.picUrl = res.data.url;
        },
        handleChange(value) {
            console.log('handleChange,'+this.selectedOptions)
            // var loc = "";
            // for (let i = 0; i < value.length; i++) {
            //   loc += CodeToText[value[i]];
            // }
            if (value != null && value.length > 1) {
                this.queryParams.addrProvince = CodeToText[value[0]];
                this.queryParams.addrCity = CodeToText[value[1]];
            }
            //console.log(loc)
        },

        // 处理添加表单中的省市cas
        handleFormAreaChange(value) {
            console.log('handleFormAreaChange')
            if (value != null && value.length > 1) {
                this.form.addrProvince = CodeToText[value[0]];
                this.form.addrCity = CodeToText[value[1]];
            }
        },

        /** 查询景区基础数据列表 */
        getList() {
            this.loading = true;
            console.log('查询景区:' + JSON.stringify(this.queryParams))
            listScenebase(this.queryParams).then(response => {
                this.scenebaseList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                description: null,
                telephone: null,
                name2: null,
                addrProvince: null,
                addrCity: null,
                addrAddr: null,
                ifShowTel: null,
                latitude: null,
                longtitude: null,
                ifShowInMainpage: null,
                ifAvailable: null,
                ifPaid: null,
                sortId: null,
                tagId: [],
                picUrl: null,
                linkAppId: null,
                linkAppPath: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                addrProvince: null,
                addrCity: null,
            }
            this.selectedOptions = []
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加景区基础数据";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            let that = this
            this.reset();
            const id = row.id || this.ids
            getScenebase(id).then(response => {
                that.form = response.data;
                that.form.tagId = this.form.tagId.split(",");
                //  that.form.selectedAreaOptions = TextToCode[that.form.addrProvince][that.form.addrCity].code
                that.form.selectedAreaOptions = []
                // console.log(TextToCode[that.form.addrProvince])
                // console.log(TextToCode[that.form.addrProvince][that.form.addrCity])
                that.$set(that.form, 'selectedAreaOptions', [TextToCode[that.form.addrProvince].code, TextToCode[that.form.addrProvince][that.form.addrCity].code])
                // console.log(that.form)
                this.open = true;
                this.title = "修改景区基础数据";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.tagId = this.form.tagId.join(",");
                    if (this.form.id != null) {
                        updateScenebase(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addScenebase(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除景区基础数据编号为"' + ids + '"的数据项？').then(function () {
                return delScenebase(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('scenebase/scenebase/export', {
                ...this.queryParams
            }, `scenebase_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
